package cn.inkur.algorithm.basic;

/**
 * <p>
 * 阶乘之和
 * </p>
 *
 * @author: Hsu
 * @date: 2024/10/20 18:00
 * @copyright: companyName
 * @see
 * @since 1.8
 */
public class SumOfFactorial {

    public static void main(String[] args) {
        System.out.println(loopSum(5));
        System.out.println(quick(5));
    }

    /**
     * 循环累加
     *
     * @param n
     * @return
     */
    public static long loopSum(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += loop(i);
        }
        return sum;
    }

    /**
     * 循环
     *
     * @param num
     * @return
     */
    public static long loop(int num) {
        long sum = 1;
        for (int i = 1; i <= num; i++) {
            sum *= i;
        }
        return sum;
    }

    /**
     * 快速幂
     *
     * @param n
     * @return
     */
    public static long quick(int n) {
        long sum = 0;
        long current = 1;
        for (int i = 1; i <= n; i++) {
            current *= i;
            sum += current;
        }

        return sum;
    }

}
